package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import application.Field;
import application.Zug;


public class GamePanel extends JPanel implements Observer,MouseListener,MouseMotionListener{

	private static final long serialVersionUID = -8860204251354754377L;
	
	private JButton     beenden;
	private JTextArea   logger;
	private JScrollPane logScroller;
	private OptionPanel optionPanel;
	private String      attackerName;
	private String      defenderName;
	private Boolean     isAttackerHuman;
	private Boolean     isDefenderHuman;
	private String[][] 	stonePositions;
	private int 		mousePosX,mousePosY;
	private Field		source, destination;
	private Zug 		zug;
	private String		fehlermeldung;
	
	

	public GamePanel(){
		this.setSize(800,600);
		GridBagLayout thisLayout = new GridBagLayout();
		this.setLayout(thisLayout);
		this.setPreferredSize(new java.awt.Dimension(800, 600));
		this.setOpaque(false);
		
//		stonePositions = new String[][]{{"L","L","L","L","S","S","S","S","S","L","L","L","L"},
//									    {"L","L","L","L","L","L","S","L","L","L","L","L","L"},
//									    {"L","L","L","L","L","L","L","L","L","L","L","L","L"},
//									    {"L","L","L","L","L","L","L","L","L","L","L","L","L"},
//									    {"S","L","L","L","L","L","W","L","L","L","L","L","S"},
//									    {"S","L","L","L","L","W","W","W","L","L","L","L","S"},
//									    {"S","S","L","L","W","W","K","W","W","L","L","S","S"},
//									    {"S","L","L","L","L","W","W","W","L","L","L","L","S"},
//									    {"S","L","L","L","L","L","W","L","L","L","L","L","S"},
//									    {"L","L","L","L","L","L","L","L","L","L","L","L","L"},
//									    {"L","L","L","L","L","L","L","L","L","L","L","L","L"},
//									    {"L","L","L","L","L","L","S","L","L","L","L","L","L"},
//									    {"L","L","L","L","S","S","S","S","S","L","L","L","L"}};
		
		addMouseListener(this);
		addMouseMotionListener(this);
		
		thisLayout.rowWeights = new double[] {0.0, 0.1, 0.0, 0.1};
		thisLayout.rowHeights = new int[] {113, 7, 277, 7};
		thisLayout.columnWeights = new double[] {0.0, 0.1, 0.1, 0.1};
		thisLayout.columnWidths = new int[] {554, 7, 7, 7};
		
		optionPanel = new OptionPanel();
		this.add(optionPanel,new GridBagConstraints(3, 2, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
		
		logger = new JTextArea(20,40);
		logger.setText("Hier werden die Spielzüge angezeigt.\n");
		logger.setEditable(false);
		logScroller = new JScrollPane(logger);
		logScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		this.add(logScroller, new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		
		beenden = new JButton("Beenden");
		this.add(beenden, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		beenden.setPreferredSize(new java.awt.Dimension(177, 57));
		
		beenden.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				GUIController.getInstance().exitGame();		
			}});
		
		optionPanel.gameStarter.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				attackerName = optionPanel.attackerOption.nameField.getText();
				defenderName = optionPanel.defenderOption.nameField.getText();
				if(optionPanel.attackerOption.attackerOption1.isSelected()){
					isAttackerHuman = true;
				}else if(optionPanel.attackerOption.attackerOption2.isSelected()){
					isAttackerHuman = false;
				}
				if(optionPanel.defenderOption.defenderOption1.isSelected()){
					isDefenderHuman = true;
				}else if(optionPanel.defenderOption.defenderOption2.isSelected()){
					isDefenderHuman = false;
				}
				GUIController.getInstance().initialisiereSpiel(attackerName,defenderName,isAttackerHuman,isDefenderHuman,optionPanel.slider.getValue());		
			}});
	
	}
	
	public Field toField(int xpos, int ypos){
		Field pos = null;
		int secondComp = -1;
		int firstComp =  -1;
		if((xpos >= 25 && xpos <= 415)){
			if(xpos * 13 / 390 == 1){
				secondComp = 0;
			}else if(xpos * 13 / 390 == 2){
				secondComp = 1;
			}else if(xpos * 13 / 390 == 3){
				secondComp = 2;
			}else if(xpos * 13 / 390 == 4){
				secondComp = 3;
			}else if(xpos * 13 / 390 == 5){
				secondComp = 4;
			}else if(xpos * 13 / 390 == 6){
				secondComp = 5;
			}else if(xpos * 13 / 390 == 7){
				secondComp = 6;
			}else if(xpos * 13 / 390 == 8){
				secondComp = 7;
			}else if(xpos * 13 / 390 == 9){
				secondComp = 8;
			}else if(xpos * 13 / 390 == 10){
				secondComp = 9;
			}else if(xpos * 13 / 390 == 11){
				secondComp = 10;
			}else if(xpos * 13 / 390 == 12){
				secondComp = 11;
			}else if(xpos * 13 / 390 == 13){
				secondComp = 12;
			}
		}else{
			secondComp = -1;
		}
		if(ypos >= 25 && ypos <= 415){
			if(ypos * 13 / 390 == 1){
				firstComp = 0;
			}else if(ypos * 13 / 390 == 2){
				firstComp = 1;
			}else if(ypos * 13 / 390 == 3){
				firstComp = 2;
			}else if(ypos * 13 / 390 == 4){
				firstComp = 3;
			}else if(ypos * 13 / 390 == 5){
				firstComp = 4;
			}else if(ypos * 13 / 390 == 6){
				firstComp = 5;
			}else if(ypos * 13 / 390 == 7){
				firstComp = 6;
			}else if(ypos * 13 / 390 == 8){
				firstComp = 7;
			}else if(ypos * 13 / 390 == 9){
				firstComp = 8;
			}else if(ypos * 13 / 390 == 10){
				firstComp = 9;
			}else if(ypos * 13 / 390 == 11){
				firstComp = 10;
			}else if(ypos * 13 / 390 == 12){
				firstComp = 11;
			}else if(ypos * 13 / 390 == 13){
				firstComp = 12;
			}
		}else{
			firstComp = -1;
		}
		pos = new Field(secondComp,firstComp);
		return pos;
	}

	public void renderBoard(Graphics g){
		int width = 13*30;
		int hight = 13*30;
		g.setColor(Color.BLACK);
		g.drawRect(25, 25, width, hight);
	}
	
	public void renderField(Graphics g,int posx,int posy){
		int width = 30;
		int hight = 30;
		g.setColor(Color.ORANGE);
		g.fillRect(posx, posy, width, hight);
		g.setColor(Color.BLACK);
		g.drawRect(posx, posy, width, hight);
		
	}
	
	public void markSpecialSpots(Graphics g,int pos[][]){
		
	}
	
	public void renderAttacker(Graphics g, String pos[][]){
		stonePositions = pos;
		for(int i = 0; i < 13; i++){
			for(int j = 0; j < 13; j++){
				if(stonePositions[i][j].equalsIgnoreCase("S")){
					int x = 27 + (j * 30);
					int y = 27 + (i * 30);
					g.setColor(Color.BLACK);
					g.fillOval(x, y, 25, 25);
				}
			}
		}
		
	}
	
	public void renderDefender(Graphics g, String pos[][]){
		stonePositions = pos;
		for(int i = 0; i < 13; i++){
			for(int j = 0; j < 13; j++){
				if(stonePositions[i][j].equalsIgnoreCase("W")){
					int x = 27 + (j * 30);
					int y = 27 + (i * 30);
					g.setColor(Color.WHITE);
					g.fillOval(x, y, 25, 25);
				}
			}
		}
		
	}
	
	public void renderKing(Graphics g, String pos[][]){
		stonePositions = pos;
		for(int i = 0; i < 13; i++){
			for(int j = 0; j < 13; j++){
				if(stonePositions[i][j].equalsIgnoreCase("K")){
					int x = 27 + (j * 30);
					int y = 27 + (i * 30);
					g.setColor(Color.WHITE);
					g.fillOval(x, y, 25, 25);
					g.setColor(Color.BLACK);
					g.drawOval(x+3, y+3, 19, 19);
					g.drawOval(x+6, y+6, 13, 13);
					g.fillOval(x+9, y+9, 7, 7);
				}
			}
		}
		
	}
	
	@Override
	public void paintComponent(Graphics g){
		renderBoard(g);
		int positionx = 25;
		int positiony = 25;
		for(int i = 0; i < 13;i++){
			for(int j = 0; j < 13; j++){
				renderField(g, positionx, positiony);
				positionx += 30;
			}
			positionx = 25;
			positiony += 30;
		}
		renderDefender(g, stonePositions);
		renderAttacker(g, stonePositions);
		renderKing(g, stonePositions);
		//markSpecialSpots(g, stonePositions);
	}
	
	@Override
	public void update(Observable o, Object arg){
		logger.append("update");
		stonePositions = (String[][]) arg;
		this.repaint();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		mousePosX=e.getX();
		mousePosY=e.getY();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		mousePosX=e.getX();
		mousePosY=e.getY();
	}
	
	@Override
	public void mousePressed(MouseEvent e) {
		source = toField(mousePosX, mousePosY);	
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		destination = toField(mousePosX, mousePosY);
		zug = new Zug(source, destination);
		try {
			fehlermeldung = GUIController.getInstance().zugKontrolle(zug);
			logger.append(fehlermeldung+"\n");
		} catch (IOException e1) {
			
			e1.printStackTrace();
			
		}
	}
	
	//werden nicht benutzt
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}

	

}
